package edu.ccut.saturn.studio.jsp;

import java.io.File;
import java.util.Map;

import org.eclipse.core.resources.IResource;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.core.util.wizard.SaturnWizardPage;

public class SaturnJspWizardPage extends SaturnWizardPage {

	private Text text_3;
	private Text text;
	private Text preViewtext;
	private Table table;
	private Button fUseTemplateButton;
	private String textValue = "";

	private static final String SATURN_JSPWZARD_TEMPLATE = "Template";
	public static final String SATURN_JSPWZARD_FILENAME = "jspFileName";

	public SaturnJspWizardPage() {
		setTitle(Messages.getString("SaturnJspWizardPage.setParemeter"));  //$NON-NLS-1$
		setDescription(Messages.getString("SaturnJspWizardPage.setJspParameter"));  //$NON-NLS-1$
	}
	
	public void initalValues(Map<String, Object> values) {
		values.put(SATURN_JSPWZARD_TEMPLATE, textValue);
		values.put(SATURN_JSPWZARD_FILENAME, text.getText());
	}

	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);
		setControl(container);
		
		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setText(Messages.getString("SaturnJspWizardPage.fileName")); //$NON-NLS-1$

		text = new Text(container, SWT.BORDER);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				text_3.setText(getComponentKey(text.getText().trim() + ".jsp"));
				validate();
			}
		});
		final Label label_1_1 = new Label(container, SWT.NONE);
		label_1_1.setText(Messages.getString("SaturnJspWizardPage.key")); //$NON-NLS-1$

		text_3 = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		text_3.setText(getComponentKey(""));
		final GridData gd_text_3 = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		gd_text_3.minimumWidth = 5;
		text_3.setLayoutData(gd_text_3);
		new Label(container, SWT.NONE);

		fUseTemplateButton = new Button(container, SWT.CHECK);
		fUseTemplateButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				enableTemplates();
			}
		});
		final GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
		fUseTemplateButton.setLayoutData(data);
		fUseTemplateButton.setSelection(true);
		fUseTemplateButton.setText("use template");
		new Label(container, SWT.NONE);

		final Composite innerParent = new Composite(container, SWT.NONE);
		final GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd.heightHint = 63;
		innerParent.setLayoutData(gd);
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 2;
		gridLayout_1.marginWidth = 0;
		gridLayout_1.marginHeight = 0;
		innerParent.setLayout(gridLayout_1);

		table = new Table(innerParent, SWT.FULL_SELECTION | SWT.BORDER);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		final GridData gd_table = new GridData(SWT.CENTER, SWT.FILL, true,
				true, 2, 1);
		gd_table.heightHint = 61;
		table.setLayoutData(gd_table);

		final TableColumn column1 = new TableColumn(table, SWT.NONE);
		column1.setWidth(234);
		column1.setText(Messages.getString("SaturnJspWizardPage.templateName")); //$NON-NLS-1$

		final TableColumn column2 = new TableColumn(table, SWT.NONE);
		column2.setWidth(232);
		column2.setText(Messages.getString("SaturnJspWizardPage.templateDescription")); //$NON-NLS-1$

		final TableItem newItemTableItem = new TableItem(table, SWT.BORDER);
		newItemTableItem.setText(new String[] { "T1", Messages.getString("SaturnJspWizardPage.template1")}); //$NON-NLS-2$
		final TableItem newItemTableItem2 = new TableItem(table, SWT.BORDER);
		newItemTableItem2.setText(new String[] { "T2", Messages.getString("SaturnJspWizardPage.template2")}); //$NON-NLS-2$
		table.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				String item = table.getItem(table.getSelectionIndex())
						.getText();
				if (item.equalsIgnoreCase("T1")) {
					preViewtext
							.setText("<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\"%>\n"
									+ "<html>\n"
									+ "<head>\n"
									+ "<title>Insert title here</title>\n"
									+ "<%@ include file=\"/jsp/include/header.jsp\"%>\n"
									+ "</head>\n"
									+ "<body>\n"
									+ "\n"
									+ "</body>\n"
									+ "</html>\n"
							);
				} else if (item.equalsIgnoreCase("T2")) {
					preViewtext
							.setText("<%@ page contentType=\"text/html; charset=UTF-8\"%>\n"
									+ "<html>\n"
									+ "<head>\n"
									+ "<title>Insert title here</title>\n"
									+ "<%@ include file=\"/extjs/include/all.jsp\"%>\n"
									+ "</head>\n"
									+ "<body>\n"
									+ "\n"
									+ "</body>\n"
									+ "</html>\n"
									+ "<script>\n"
									+ "Ext.onReady(function(){\n"
									+ "\n"
									+ "\n"
									+ "});\n"
									+ "</script>"
						  );
				}
				textValue = preViewtext.getText();
			}
		});
		new Label(container, SWT.NONE);

		final Label previewLabel = new Label(container, SWT.NONE);
		previewLabel.setLayoutData(new GridData());
		previewLabel.setText(Messages.getString("SaturnJspWizardPage.perview")); //$NON-NLS-1$
		new Label(container, SWT.NONE);

		this.preViewtext = new Text(container, SWT.V_SCROLL | SWT.BORDER);
		final GridData gd_text = new GridData(SWT.FILL, SWT.BOTTOM, true, true);
		gd_text.heightHint = 83;
		gd_text.widthHint = 459;
		this.preViewtext.setLayoutData(gd_text);
	}

	void enableTemplates() {
		boolean enabled = fUseTemplateButton.getSelection();
		preViewtext.setText("");
		textValue = "";
		table.setEnabled(enabled);
		preViewtext.setEnabled(enabled);
	}

	protected String getComponentKey(String simpleKey) {
		String packageName = this.getPackageName();

		String spacename = ""; //$NON-NLS-1$
		try {
			spacename = SaturnProjectUtil.getProject(getSelection()).getName() + "/"
					+ packageName + "/" + simpleKey;

		} catch (Exception e1) {
			e1.printStackTrace();
		}

		return spacename;
	}

	protected String getPackageName() {
		IResource resource = SaturnProjectUtil.getResource(super.getSelection());

		String packageName = resource.getProjectRelativePath().toOSString();
		packageName = packageName
				.substring(packageName.indexOf(File.separator) + 1);
		packageName = packageName.replace(File.separator, "/");

		return packageName;
	}

	public boolean validate() {
		String fileName = this.text.getText();

		setPageComplete(false);

		if (SaturnValidation.isEmpty(fileName)) {
			setErrorMessage(Messages.getString("SaturnJspWizardPage.NameIsEmpty")); //$NON-NLS-1$
			return false;
		} else if (SaturnValidation.isBadWord(fileName)) {
			setErrorMessage(Messages.getString("SaturnJspWizardPage.NameContainChar")); //$NON-NLS-1$
			return false;
		} else if (new File(getFullPath() + "/" + fileName + ".jsp").exists()) {
			setErrorMessage(Messages.getString("SaturnJspWizardPage.NameExit"));  //$NON-NLS-1$
			return false;
		};
		setPageComplete(true);
		setErrorMessage(null);
		return true;

	}

}
